import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:get/get.dart';

import '../../../bean/bean_goods.dart';
import 'controller/goods_center_controller.dart';

class GoodsListPage extends StatefulWidget {
  final String tag;

  const GoodsListPage({Key key, this.tag}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return GoodsListPageState();
  }
}

class GoodsListPageState extends State<GoodsListPage> {
  GoodsCenterController goodsCenterController =
      Get.put(GoodsCenterController());

  @override
  void initState() {
    super.initState();
    goodsCenterController.requestGoodsList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("商品中心"),
      ),
      body: RefreshIndicator(
        onRefresh: () {
          return Get.find<GoodsCenterController>().onRefresh();
        },
        child: GetBuilder(
          id: "goodsList",
          builder: (GoodsCenterController controller) {
            List<GoodsBean> goodsList = controller.goodsList;
            if (goodsList.length == 0) {
              return SizedBox(
                width: double.infinity,
                height: Get.height / 3 * 2,
                child: Center(
                  child: NoDataWidget(
                    text: "暂无可兑换商品",
                  ),
                ),
              );
            }
            return Container(
              child: ListView.builder(
                itemCount: goodsList.length,
                itemBuilder: (BuildContext context, int index) {
                  GoodsBean item = goodsList[index];
                  return Container(
                    color: Colors.white,
                    margin: EdgeInsets.only(top: 1),
                    padding: EdgeInsets.only(
                        left: 12, right: 12, top: 10, bottom: 12),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        CachedNetworkImage(
                          imageUrl: item.goodsImage,
                          width: 100,
                          height: 100,
                          fit: BoxFit.fill,
                        ),
                        SizedBox(
                          width: 12,
                        ),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                "${item.goodsName}",
                                style: TextStyle(
                                    color: Color(0xff333333),
                                    fontSize: 16,
                                    fontWeight: FontWeight.w500),
                              ),
                              SizedBox(
                                height: 4,
                              ),
                              Text(
                                item.goodsContent,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style:
                                    TextStyle(fontSize: 12, color: Colors.grey),
                              ),
                              item.goodsDemand != 0
                                  ? Text(
                                      "账户可用积分需要满足${item.goodsDemand}分才可以兑换",
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(
                                          fontSize: 12,
                                          color: Colors.deepOrangeAccent),
                                    )
                                  : SizedBox(),
                              SizedBox(
                                height: 10,
                              ),
                              GetBuilder(
                                id: "submitOrder${item.id}",
                                builder: (GoodsCenterController controller) {
                                  if (controller.submitOrderLoading) {
                                    return LoadingWidget(
                                      text: "订单处理中",
                                      loadingHeight: 103,
                                      loadingTextSize: 12,
                                    );
                                  }
                                  return buildBootomWidget(item);
                                },
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            );
          },
        ),
      ),
    );
  }

  buildBootomWidget(GoodsBean item) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Row(
          children: [
            Expanded(
              child: RichText(
                text: TextSpan(text: "", children: [
                  TextSpan(
                    text: "${item.unitNumber}",
                    style: TextStyle(
                        color: Colors.red,
                        fontSize: 18,
                        fontWeight: FontWeight.w600),
                  ),
                  TextSpan(
                    text: "  积分",
                    style: TextStyle(
                        color: Colors.red,
                        fontSize: 12,
                        fontWeight: FontWeight.w400),
                  ),
                ]),
              ),
            )
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            GestureDetector(
              onTap: () {
                Get.find<GoodsCenterController>()
                    .goShop(item, widget.tag, context);
              },
              child: ClipRRect(
                child: Container(
                  padding:
                      EdgeInsets.only(left: 6, right: 6, top: 1, bottom: 2),
                  color: Colors.white,
                  child: Text(
                    "立即兑换",
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: Colors.red),
                  ),
                ),
                borderRadius: BorderRadius.all(Radius.circular(4)),
              ),
            )
          ],
        )
      ],
    );
  }
}
